package com.itheima.service.impl;


import com.itheima.exception.DeleteException;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl  implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<Dept> findAll() {

        return deptMapper.findAll();
    }
    @Override
    public void deleteById(Integer id) throws DeleteException {
        Integer count = empMapper.getCountById(id);
        if(count>0){
            throw new DeleteException("对不起，当前部门下有员工，不能直接删除！");
        }else{
            deptMapper.deleteById(id);
        }
    }
    @Override
    public void add(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        dept.setCreateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }
    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
    @Override
    public Dept findById(Integer id) {

        return  deptMapper.findById(id);
    }
}
